package com.zp.servlet;

import com.google.gson.Gson;
import com.zp.utils.ResultUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/user/quit")
public class QuitServlet extends HttpServlet {

    private PrintWriter out;

    @Override
    public void init() throws ServletException {

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        out = response.getWriter();
        Gson gson = new Gson();

        HttpSession session = request.getSession();
        try {
            session.removeAttribute("CURRENT_USER");
        } catch (Exception e) {
            e.printStackTrace();
            out.print(gson.toJson(ResultUtil.error("登出失败")));
            return;
        }
        out.print(gson.toJson(ResultUtil.success()));
    }
}
